package com.haierCamera.customapplication.utils.android;

import android.content.Context;
import android.util.SparseArray;
import android.widget.Toast;

import com.haierCamera.customapplication.Constants;
import com.haierCamera.customapplication.common.Resource;
import com.haierCamera.customapplication.ui.login.HZKLLoginActivity;
import com.haierCamera.customapplication.utils.AppExecutors;
import com.haierCamera.customapplication.utils.SharedPreferencesUtils;


import java.util.concurrent.Executor;

import javax.inject.Inject;
import javax.inject.Singleton;

/**
 * 暂时先这样
 *
 * @author yawei
 * @data on 2018/7/27 下午2:52
 * @email zyawei@live.com
 */

@Singleton
public class HttpErrorProcess {


    private final Executor appExecutors;
    private final Context application;
    private SparseArray<Runnable> runnableSparseArray;


    @Inject
    HttpErrorProcess(AppExecutors appExecutors, Context application) {
        this.appExecutors = appExecutors.mainThread();
        this.application = application;
    }

    public HttpErrorProcess(Executor appExecutors, Context context) {
        this.appExecutors = appExecutors;
        this.application = context;
    }


    public HttpErrorProcess register(int code, Runnable runnable) {
        if (null == runnableSparseArray) {
            runnableSparseArray = new SparseArray<>(1);
        }
        runnableSparseArray.append(code, runnable);
        return this;
    }

    public void process(int code, String message) {
        Runnable runnable = null;
        if (runnableSparseArray != null) {
            runnable = runnableSparseArray.get(code);
            runnableSparseArray.delete(code);
        }
        if (runnable == null) {
            switch (code) {
                case 0:
                    return;
                case 400://参数错误
                    runnable = () -> Toast.makeText(application, "服务器异常(400)", Toast.LENGTH_SHORT).show();
                    break;
                case 404:
                    runnable = () -> Toast.makeText(application, "服务器异常(404)", Toast.LENGTH_SHORT).show();
                    break;
                case 500:
                    runnable = () -> Toast.makeText(application, "服务器异常(500)", Toast.LENGTH_SHORT).show();
                    break;
                case 20000:
                    runnable = () -> {
                        // TODO: 2018/8/23
                        Toast.makeText(application, "用户信息过期，请重新登陆..", Toast.LENGTH_SHORT).show();

                        SharedPreferencesUtils.removeSync(Constants.SP_USER_TOKEN);
                        ActivityManager.getInstance().starWithClearTask(HZKLLoginActivity.class, null);

                    };
                    break;

                default:
                    runnable = () -> Toast.makeText(application, message, Toast.LENGTH_SHORT).show();
                    break;
            }
        }
        appExecutors.execute(runnable);
    }

    public void process(Resource resource) {
        process(resource.errorCode, resource.errorMessage);
    }
}
